package com.hptel.timberwood.test;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.timberwood.model.Category;
import com.timberwood.model.Products;

public class Main {

	public static void main(String[] args) {

		//Session session = HibernateUtil.getSessionFactory().openSession();
		Session session = new Configuration().configure().buildSessionFactory().openSession();
		Transaction transaction = null;
		try {

			transaction = session.beginTransaction();
			
			/*
			 * These lines which insert a new Products into database and add it on Category
			 * Steps:
			 * 1.Create a Product object
			 * 2.Select Category from database with specified Cat ID
			 * 3.Add products to Category
			 * 4.Update Category 
			 */
			//Products pr = new Products("Mango", "Mgo");
			/*Products pr = new Products("Oranges", "Org");
			int cateid = 1;
			
			Query query5 = session.getNamedQuery("findCategoryByID").setInteger("categoryID", cateid);
			Category category = (Category) query5.uniqueResult();
			Set<Products> cate = category.getProducts();
			if(!cate.contains(pr)){
				cate.add(pr);
				category.setProducts(cate);
				session.update(category);
			}
			*/
			
			/*
			 * These lines which delete a Product from database with ProductID
			 * Steps:
			 * 
			 */
			/*int productID = 4;
			Query query6 = session.getNamedQuery("findProductByID").setInteger("productID", productID);
			Products delProd = (Products) query6.uniqueResult();
			
			Query query7 = session.getNamedQuery("listAllCategories");
			Iterator<Category> categoryList = query7.list().iterator();
			Set<Products> productSet = null;
			
			Category temp = null;
			while(categoryList.hasNext()){
				temp = categoryList.next();
				productSet = temp.getProducts();
				if(productSet.contains(delProd)){
					productSet.remove(delProd);
					session.update(temp);
					session.delete(delProd);
				}
			}
			*/
			
			Query query = session.getNamedQuery("listLastestRecords");
			query.setMaxResults(6);
			Set<Products> prods = new HashSet<Products>(query.list());
			
			System.out.println("test ==== "  + prods.size());
			
			//Remove a product from a category
			//1.Select all category
			//2.for each category: find the category which has the deleting products
			
			
			
			
			/*
			    These lines which insert a new Category with several Products
			  
			    Set<Products> products = new HashSet<Products>();
			    products.add(new Products("Coffees","C11"));
				products.add(new Products("Water mellon","W11"));
			
				Set<Products> products2 = new HashSet<Products>();
				products2.add(new Products("monitor","M1"));
				products2.add(new Products("Chips","I1"));
			
				Category category = new Category("Computer Devices", products2);
				session.save(category);
			*/
			
		 
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

	}
}
